package com.sundi.ecmarket.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundi.ecmarket.config.ExcelBean;
import com.sundi.ecmarket.config.ExcelUtils;
import com.sundi.ecmarket.dto.BrandDto;
import com.sundi.ecmarket.dto.KeywordDto;
import com.sundi.ecmarket.entity.EcBrand;
import com.sundi.ecmarket.entity.EcIssue;
import com.sundi.ecmarket.entity.EcKeyword;
import com.sundi.ecmarket.mapper.EcKeywordMapper;
import com.sundi.ecmarket.service.IEcKeywordService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 关键字表 服务实现类
 * </p>
 *
 * @author Mht
 * @since 2020-02-23
 */
@Service
public class EcKeywordServiceImpl extends ServiceImpl<EcKeywordMapper, EcKeyword> implements IEcKeywordService {

    @Override
    public List<EcKeyword> selectKeywordList(KeywordDto keywordDto) {
        return this.baseMapper.selectKeywordList(keywordDto);
    }

    @Override
    public boolean deleteByKey(Integer id) {
        return this.baseMapper.deleteByKey(id);
    }

    @Override
    public XSSFWorkbook downLoadGoodsExcel(KeywordDto keywordDto) throws Exception {
        PageHelper.startPage(keywordDto.getPageNo(),keywordDto.getPageSize());
        Wrapper wrapper = new EntityWrapper();

        if (StringUtils.isNotBlank(keywordDto.getKeyword())){
            wrapper.like("keyword",keywordDto.getKeyword());
        }
        List<EcKeyword> list = this.selectList(wrapper);
        PageInfo<EcKeyword> pageInfo = new PageInfo(list);
        Map<Integer,List<ExcelBean>> map = new HashMap<Integer,List<ExcelBean>>();
        List<ExcelBean> excelBeans = new ArrayList<>();
        excelBeans.add(new ExcelBean("id","id",0));
        excelBeans.add(new ExcelBean("关键字","keyword",0));
        excelBeans.add(new ExcelBean("关键字的跳转链接","url",0));
        excelBeans.add(new ExcelBean("是否是热门关键字","isHot",0));
        excelBeans.add(new ExcelBean("是否是默认关键字","isDefault",0));
        excelBeans.add(new ExcelBean("排序","sortOrder",0));
        map.put(0,excelBeans);

        return ExcelUtils.createExcelFile(EcKeyword.class,pageInfo.getList(),map,"商铺列表");
    }

    @Override
    public void uplodExcel(MultipartFile multipartFile) throws Exception {
        List<List<Object>> bankListByExcel = ExcelUtils.getBankListByExcel(multipartFile.getInputStream(), multipartFile.getOriginalFilename());
        List<EcKeyword> list = new ArrayList<>();

        for (List<Object> objects : bankListByExcel) {
            EcKeyword ecKeyword = new EcKeyword();
            ecKeyword.setKeyword(objects.get(1).toString());
            ecKeyword.setUrl(objects.get(2).toString());
            ecKeyword.setIsDefault(Integer.valueOf(objects.get(3).toString()));
            ecKeyword.setIsHot(Integer.valueOf(objects.get(3).toString()));
            list.add(ecKeyword);
        }
        this.insertBatch(list);
    }
}
